AC_DEFUN([LX_CHECK_LEMON],
[
  AC_ARG_WITH([lemon],
  AS_HELP_STRING([--with-lemon@<:@=PREFIX@:>@], 
                 [search for LEMON under PREFIX or under the default search paths if PREFIX is not given @<:@default@:>@])
AS_HELP_STRING([--without-lemon], [disable checking for LEMON]),
              [], [with_lemon=yes])

  AC_ARG_WITH([lemon-includedir],
  AS_HELP_STRING([--with-lemon-includedir=DIR], 
                 [search for LEMON headers in DIR]),[], 
                 [with_lemon_includedir=no])

  AC_ARG_WITH([lemon-libdir],
  AS_HELP_STRING([--with-lemon-libdir=DIR], 
                 [search for LEMON libraries in DIR]),[], 
                 [with_lemon_libdir=no])

  lx_lemon_found=no

  AC_MSG_CHECKING([for LEMON])

  if test x"$with_lemon_includedir" != x"no"; then
    LEMON_CFLAGS="-I$with_lemon_includedir"
  elif test x"$with_lemon" != x"yes"; then
    LEMON_CFLAGS="-I$with_lemon/include"
  fi

  if test x"$with_lemon_libdir" != x"no"; then
    LEMON_LDFLAGS="-L$with_lemon_libdir"
  elif test x"$with_lemon" != x"yes"; then
    LEMON_LDFLAGS="-L$with_lemon/lib"
  fi
  LEMON_LIBS="-lemon"

  lx_save_cxxflags="$CXXFLAGS"
  lx_save_ldflags="$LDFLAGS"
  lx_save_libs="$LIBS"
  CXXFLAGS="$LEMON_CFLAGS"
  LDFLAGS="$LEMON_LDFLAGS"
  LIBS="$LEMON_LIBS"

  lx_lemon_test_prog='
    #include <lemon/list_graph.h>

    int main(int argc, char** argv)
    {
      lemon::ListGraph g;
      return 0;
    }'

  AC_LANG_PUSH(C++)
  AC_LINK_IFELSE([$lx_lemon_test_prog], 
		 [lx_lemon_found=yes], [lx_lemon_found=no]) 

  AC_LANG_POP(C++)

  CXXFLAGS="$lx_save_cxxflags"
  LDFLAGS="$lx_save_ldflags"
  LIBS="$lx_save_libs"

  if test x"$lx_lemon_found" = x"no"; then
    AC_MSG_ERROR([LEMON missing.])
  fi

  AC_DEFINE([HAVE_LEMON], [1], [Define to 1 if you have LEMON.])
  AC_MSG_RESULT([yes])

  LEMON_LIBS="$LEMON_LDFLAGS $LEMON_LIBS"
  AC_SUBST(LEMON_CFLAGS)
  AC_SUBST(LEMON_LIBS)
  AM_CONDITIONAL([HAVE_LEMON], [test x"$lx_lemon_found" = x"yes"])
])
